<?php

class AgenController extends BaseController {
//agen
	public function postAgen()
	{
		

		if(Input::get('filter')=='date'){
			Session::forget('list');
			Session::forget('filterby');
			Session::forget('keyword');
			Session::put('filter','date');
				if(Input::get('datestart')==""){
					return Redirect::back()->with('error', "<font color='red'> Harap masukan tanggal</font><br>");
				}
				if(Input::get('dateend')==""){
					return Redirect::back()->with('error', "<font color='red'> Harap masukan tanggal</font><br>");
				}
			Session::put('datestart',Input::get('datestart'));
			Session::put('dateend',Input::get('dateend'));
			$dataagen = DB::table('sys_agen')
					->join('sys_member','member_agen_id','=','agen_id')
					->join('ref_provinsi','member_provinsi_id','=','provinsi_id')
					->join('ref_regional','member_regional_id','=','regional_id')
					->whereBetween('member_joindate', array(date("Y-m-d h:m:s",strtotime(Input::get('datestart'))), date("Y-m-d h:m:s",strtotime(Input::get('dateend')))))
					->paginate(10);
		}else if(Input::get('filter')=='type'){
			Session::forget('filterby');
			Session::forget('keyword');
			Session::put('filter','type');
			Session::put('list', Input::get('typeby'));
			if(Input::get('typeby')=='all'){
				$dataagen = DB::table('sys_agen')
					->join('sys_member','member_agen_id','=','agen_id')
					->join('ref_provinsi','member_provinsi_id','=','provinsi_id')
					->join('ref_regional','member_regional_id','=','regional_id')
					->paginate(10);
			}else {
				$dataagen = DB::table('sys_agen')
					->join('sys_member','member_agen_id','=','agen_id')
					->join('ref_provinsi','member_provinsi_id','=','provinsi_id')
					->join('ref_regional','member_regional_id','=','regional_id')
					->where('member_status','=',Input::get('typeby'))
					->paginate(10);
			}
		}else if(Input::get('filter')=='keyword'){
			Session::put('filter','keyword');
			Session::forget('list');
			Session::put('filterby',Input::get('filterby'));
			Session::put('keyword',Input::get('keyword'));
			if(Input::get('filterby')=='agencode'){
			$dataagen = DB::table('sys_agen')
					->join('sys_member','member_agen_id','=','agen_id')
					->join('ref_provinsi','member_provinsi_id','=','provinsi_id')
					->join('ref_regional','member_regional_id','=','regional_id')
					->where('agen_code','LIKE','%'.Input::get('keyword').'%')
					->paginate(10);	
			}else if(Input::get('filterby')=='agenname'){
			$dataagen = DB::table('sys_agen')
					->join('sys_member','member_agen_id','=','agen_id')
					->join('ref_provinsi','member_provinsi_id','=','provinsi_id')
					->join('ref_regional','member_regional_id','=','regional_id')
					->where('member_name','LIKE','%'.Input::get('keyword').'%')
					->paginate(10);
			}else if(Input::get('filterby')=='referalcode'){	
			$dataagen = DB::table('sys_agen')
					->join('sys_member','member_agen_id','=','agen_id')
					->join('ref_provinsi','member_provinsi_id','=','provinsi_id')
					->join('ref_regional','member_regional_id','=','regional_id')
					->leftjoin('sys_agen AS ch','ch.agen_id','=','sys_agen.agen_sponsor_id')
					->where('ch.agen_code','LIKE','%'.Input::get('keyword').'%')
					->groupBy('sys_agen.agen_id')
					->select('sys_agen.agen_code','sys_agen.agen_sponsor_id','sys_agen.agen_id','member_name','member_status','member_email','member_phone','member_address','member_joindate','member_regional_id','member_provinsi_id','regional_name','provinsi_name')
					->paginate(10);
			}			
		}else{
        	if(Session::get('filter')=="date"){
        			$dataagen = DB::table('sys_agen')
							->join('sys_member','member_agen_id','=','agen_id')
							->join('ref_provinsi','member_provinsi_id','=','provinsi_id')
							->join('ref_regional','member_regional_id','=','regional_id')
							->whereBetween('member_joindate', array(date("Y-m-d h:m:s",strtotime(Session::get('datestart'))), date("Y-m-d h:m:s",strtotime(Session::get('dateend')))))
							->paginate(10);
        	}else if(Session::get('filter')=="type"){
        			if(Session::get('list')=='all'){
						$dataagen = DB::table('sys_agen')
							->join('sys_member','member_agen_id','=','agen_id')
							->join('ref_provinsi','member_provinsi_id','=','provinsi_id')
							->join('ref_regional','member_regional_id','=','regional_id')
							->paginate(10);
					}else {
						$dataagen = DB::table('sys_agen')
							->join('sys_member','member_agen_id','=','agen_id')
							->join('ref_provinsi','member_provinsi_id','=','provinsi_id')
							->join('ref_regional','member_regional_id','=','regional_id')
							->where('member_status','=',Session::get('list'))
							->paginate(10);
					}
        	}else if(Session::get('filter')=="keyword"){
        			if(Session::get('filterby')=='agencode'){
					$dataagen = DB::table('sys_agen')
							->join('sys_member','member_agen_id','=','agen_id')
							->join('ref_provinsi','member_provinsi_id','=','provinsi_id')
							->join('ref_regional','member_regional_id','=','regional_id')
							->where('agen_code','LIKE','%'.Session::get('keyword').'%')
							->paginate(10);	
					}else if(Session::get('filterby')=='agenname'){
					$dataagen = DB::table('sys_agen')
							->join('sys_member','member_agen_id','=','agen_id')
							->join('ref_provinsi','member_provinsi_id','=','provinsi_id')
							->join('ref_regional','member_regional_id','=','regional_id')
							->where('member_name','LIKE','%'.Session::get('keyword').'%')
							->paginate(10);
					}else if(Session::get('filterby')=='referalcode'){	
					$dataagen = DB::table('sys_agen')
							->join('sys_member','member_agen_id','=','agen_id')
							->join('ref_provinsi','member_provinsi_id','=','provinsi_id')
							->join('ref_regional','member_regional_id','=','regional_id')
							->leftjoin('sys_agen AS ch','ch.agen_id','=','sys_agen.agen_sponsor_id')
							->where('ch.agen_code','LIKE','%'.Session::get('keyword').'%')
							->groupBy('sys_agen.agen_id')
							->select('sys_agen.agen_code','sys_agen.agen_sponsor_id','sys_agen.agen_id','member_name','member_status','member_email','member_phone','member_address','member_joindate','member_regional_id','member_provinsi_id','regional_name','provinsi_name')
							->paginate(10);
					}
        	}
        }			

		$provinsi = provinsi::all();
		$regional = regional::all();
		return View::make('admin.agen.agen',compact('provinsi',$provinsi,'regional',$regional,'dataagen',$dataagen));
	}

	public function getLoginagen($id)
	{
		$agen = DB::table('sys_account_agen')
					->where('account_agen_id',$id)
					->first();
		if(Auth::user()->attempt(array(
			'account_username' => $agen->account_username,
			'password' => Input::get('password')
		))){
		$user = Auth::user()->get();
		Session::put('account_agen_id',$user->account_agen_id);
		Session::put('account_username',$user->account_username);

		$log = New adminlog();
		$log->admin_log_admin_id = Session::get('admin_id');	
		$log->admin_log_activity = "Login agen ".$user->account_agen_id;	
		$log->admin_log_date = date('Y-m-d H:i:s');	
		$log->save();
		return Redirect::to('/virtualoffice')->with('message', "Selamat datang ".$user->account_username);
			
		} else {
			return Redirect::back()->with('error', "<font color='red'> Username / password salah</font><br>");
		}
	}

	public function getAgenbonus($id)
	{
		$t = getdate();
        $t = $t[0];
        $t = date('Y', $t);

        $b = getdate();
        $b = $b[0];
        $b = date('m', $b);

        $d = getdate();
        $d = $d[0];
        $d = date('d', $d);

        $komisirefd = bonuslog::find($id)->whereBetween('bonus_log_date', array($t . '-' . $b . '-' . $d . ' 00:00:00', $t . '-' . $b . '-' . $d . ' 23:59:59'))->sum('bonus_log_referal');
        $komisiordd = bonuslog::find($id)->whereBetween('bonus_log_date', array($t . '-' . $b . '-' . $d . ' 00:00:00', $t . '-' . $b . '-' . $d . ' 23:59:59'))->sum('bonus_log_order');
        $komisireferal = DB::table('sys_bonus_log')
                ->where('bonus_log_agen_id', $id)
                ->where('bonus_log_referal', '<>', 0)
                ->get();


        $komisiorder = DB::table('sys_bonus_log')
                ->where('bonus_log_agen_id', $id)
                ->where('bonus_log_order', '<>', 0)
                ->get();
        $sumkomisiorder = DB::table('sys_bonus_log')
                ->where('bonus_log_agen_id', $id)
                ->where('bonus_log_order', '<>', 0)
                ->sum('bonus_log_order');

        // echo'<pre>';
        // print_r($komisiall);	
        // echo'<pre>';

        $komisi = bonus::find($id);
        $komisi->bonus_order = $sumkomisiorder;
        $komisi->Save();

        $agen = agen::find($id);
        $agenid = $agen->agen_code;

        return View::make('admin.agen.bonus', compact('agenid',$agenid,'komisi', $komisi, 'komisiorder', $komisiorder, 'komisireferal', $komisireferal, 'komisiordd', $komisiordd, 'komisirefd', $komisirefd));
	}


	// public function postAgenbonus($id)
	// {
	// 	$databonus = refbonus::find($id);
	// 	$databonus->bonus_config_percentage = Input::get('bonus');
	// 	$databonus->save();

	// 	$log = New adminlog();
	// 	$log->admin_log_admin_id = Session::get('admin_id');	
	// 	$log->admin_log_activity = "Edit bonus";	
	// 	$log->admin_log_date = date('Y-m-d H:i:s');	
	// 	$log->save();
	// 	return Redirect::to('/agen/bonus')->with('message','Bonus berhasil diganti');
	// }

	public function getAgen()
	{
		Session::forget('list');
		Session::forget('filter');
		Session::forget('filterby');
		Session::forget('keyword');
		Session::forget('datestart');
		Session::forget('dateend');
		$dataagen = DB::table('sys_agen')
					->join('sys_member','member_agen_id','=','agen_id')
					->join('ref_provinsi','member_provinsi_id','=','provinsi_id')
					->join('ref_regional','member_regional_id','=','regional_id')
					->paginate(10);
		$provinsi = provinsi::all();
		$regional = regional::all();
		return View::make('admin.agen.agen',compact('provinsi',$provinsi,'regional',$regional,'dataagen',$dataagen));
	}



	public function postEditpasswordagen($id)
	{
		$rules = array(
				'password' => 'required|confirmed',
				'password_confirmation' => 'required',
			);	
			$notif = array(
				'confirmed' => ' <font color="red"> Harap masukan pasword yang sama.</font><br>',);

			$validator = Validator::make(Input::all(), $rules, $notif);

			if($validator->fails()){
				return Redirect::back()->with('error','Harap masukan pasword yang sama.');
			}
			$agen = accountagen::find($id);
			$agen->password = Hash::make(Input::get('password'));
			$agen->save();

			$log = New adminlog();
			$log->admin_log_admin_id = Session::get('admin_id');	
			$log->admin_log_activity = "Edit password agen";	
			$log->admin_log_date = date('Y-m-d H:i:s');	
			$log->save();

			$agens = DB::table('sys_member')
				->where('member_agen_id',$id)
				->first();
			$user = array(
			  'email'=>$agens->member_email,
			);
			//send mail
			Mail::send('emails.agenreset', array('pesan'=>'Acount agen anda telah aktif, Terimakasih.','username'=>$agens->member_email,'password'=>Input::get('password')), function($message) use ($user){
			$message->to($user['email'])->subject('Pasword agen reset');
	    	});
		return Redirect::to('/agen')->with('message','Password berhasil diganti');
	}

	public function postEditprofileagen($id)
	{
		$member = member::Find($id);
		$member->member_name = Input::get('member_name');
		$member->member_address =  Input::get('member_address');
		$member->member_phone = Input::get('member_phone');
		$member->member_email = Input::get('member_email');
		$member->member_regional_id = Input::get('member_regional_id');
		$member->member_provinsi_id = Input::get('member_provinsi_id');
		$member->save();

		$log = New adminlog();
		$log->admin_log_admin_id = Session::get('admin_id');	
		$log->admin_log_activity = "Edit profil agen";	
		$log->admin_log_date = date('Y-m-d H:i:s');	
		$log->save();
		return Redirect::to('/agen')->with('message','Profile berhasil diubah');

	}

	public function postAktifagen()
	{

		$agen = DB::table('sys_member')
				->get();
		$a = 0;
		foreach($agen as $agen){
		if(Input::get($agen->member_agen_id)=='1'){
			$id = Input::get($agen->member_agen_id);
			$a = $a + Input::get($agen->member_agen_id);
			$agenc = DB::table('sys_account_agen')
					->where('account_agen_id','=',$agen->member_agen_id)
					->Count();
			if($agenc==0){
					$accountagen = New accountagen();
					$accountagen->account_agen_id = $agen->member_agen_id;
					$accountagen->account_username = $agen->member_email;
					$accountagen->password =  Hash::make('default'.$agen->member_agen_id);
					$accountagen->save();
			$log = New adminlog();
			$log->admin_log_admin_id = Session::get('admin_id');	
			$log->admin_log_activity = "Aktifasi account baru agen ".$agen->member_agen_id;	
			$log->admin_log_date = date('Y-m-d H:i:s');	
			$log->save();

			$agens = DB::table('sys_member')
					->where('member_agen_id','=',$agen->member_agen_id)
					->update(array('member_status' => 'Aktif'));
			$user = array(
			  'email'=>$agen->member_email
			);
			//send mail
			Mail::send('emails.agenaktif', array('pesan'=>'Acount agen anda telah aktif, Terimakasih.','username'=>$agen->member_email,'password'=>'default'.$agen->member_agen_id), function($message) use ($user){
			$message->to($user['email'])->subject('Account aktif');
	    	});


			return Redirect::to('/agen')->with('message','Agen telah aktif dengan password "default + agen_id"');

			}
			$agens = DB::table('sys_member')
					->where('member_agen_id','=',$agen->member_agen_id)
					->update(array('member_status' => 'Aktif'));

			$log = New adminlog();
			$log->admin_log_admin_id = Session::get('admin_id');	
			$log->admin_log_activity = "Aktifasi agen ".$agen->member_agen_id;	
			$log->admin_log_date = date('Y-m-d H:i:s');	
			$log->save();

			$user = array(
			  'email'=>$agen->member_email
			);
			//send mail
			Mail::send('emails.agenaktiflagi', array('pesan'=>'Terimakasih, account agen anda telah aktif kembali.'), function($message) use ($user){
			$message->to($user['email'])->subject('Account telah aktif');
	    	});
		}
		}
		if($a<>0){
		return Redirect::to('/agen')->with('message','Agen telh aktif');
		}
		return Redirect::to('/agen')->with('error','Silahkan pilih agen');
		
	}

	public function postNonaktifagen()
	{

		$agen = DB::table('sys_member')
				->get();
		foreach($agen as $agen){
		if(Input::get($agen->member_agen_id)=='1'){
			$id = Input::get($agen->member_agen_id);
			$agen = DB::table('sys_member')
					->where('member_agen_id','=',$agen->member_agen_id)
					->update(array('member_status' => 'Tidak aktif'));
		}
		}
			$log = New adminlog();
			$log->admin_log_admin_id = Session::get('admin_id');	
			$log->admin_log_activity = "Nonaktifasi agen";	
			$log->admin_log_date = date('Y-m-d H:i:s');	
			$log->save();

			$user = array(
			  'email'=>$agen->member_email
			);
			//send mail
			Mail::send('emails.agennonaktif', array('pesan'=>'Maaf account agen anda kami non aktifkan.'), function($message) use ($user){
			$message->to($user['email'])->subject('Account non aktif');
	    	});
		return Redirect::to('/agen')->with('message','Agen di non aktifkan');
	}

	public function getAgenlogin($id)
	{
			$user = accountagen::find($id);
			Session::put('agenlogin',$id);
		 	Session::put('account_agen_id', $user->account_agen_id);
            Session::put('account_username', $user->account_username);
            return Redirect::to('/virtualoffice')->with('message', "Selamat datang admin dengan user agen " . $user->account_username);

	}


}